<template>
  <div class="app-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="旧密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="ruleForm.newpassword"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmnewpassword">
        <el-input v-model="ruleForm.confirmnewpassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { updatePwd} from "@/api/updatePwd";
export default {
  computed: {
    ...mapGetters(["uid", "updatepath", "token"]),
  },
  filters: {},
  data() {
    var checkPassword = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('密码不能为空'));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.confirmnewpassword !== '') {
          this.$refs.ruleForm.validateField('confirmnewpassword');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newpassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "",
        newpassword: "",
        confirmnewpassword: ""
      },
      rules: {
        password: [{ validator:checkPassword, trigger: "blur" }],
        newpassword: [{ validator: validatePass , trigger: "blur" }],
        confirmnewpassword: [{ validator: validatePass2 , trigger: "blur" }],
      },
    };
  },
  created() {
    this.clearValue()
  },
  methods: {
    clearValue(){
      this.ruleForm = {
        newpassword: "",
        password: "",
        confirmnewpassword: ""
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            ...this.ruleForm,
            id: this.uid
          };
          console.log(params);

          updatePwd(params).then((res) => {
            if (res.status == 200) {
              this.$message.success(res.msg);
              this.clearValue()

            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.filter-item {
  margin-left: 4px;
  margin-right: 20px;
}
.avaimg {
  width: 50px;
  height: 50px;
}
</style>

